package csm.web.core.search;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom.Element;
import org.jdom.JDOMException;

import csm.common.Logger;
import csm.common.xml.XmlDocument;



public class MashupItemList {
	private static MashupItemList _instance = null;

	public synchronized static MashupItemList getInstance(String configFilePath ){
		if( _instance == null)
			_instance = new MashupItemList(configFilePath);
		return _instance;
	}

	public static MashupItemList getInstance(){
		if( _instance == null)
			_instance = new MashupItemList();
		return _instance;
	}

	private String configFile = "/WEB-INF/Mashup.xml";
	private List<MashupItem> items;
	private Map<String, MashupItem> mashupMap;

	/**
	 * 클래스에 내장된 기본경로(<code>/WEB-INF/Mashup.xml</code>)를 사용해서 클래스를 생성합니다.
	 */
	public MashupItemList(  )
	{
		parse();
	}
	/**
	 * 지정괸 경로를 이용해서 클래스를 생성합니다.
	 * @param configFilePath
	 */
	public MashupItemList( String configFilePath  )
	{
		this.configFile = configFilePath;
		parse();
	}

	private void parse()
	{
		Logger.info("Loading Mash-up Information form " + this.configFile);

		XmlDocument xml = null;
		try {
			xml = new XmlDocument(new File(this.configFile));
			items = new ArrayList<MashupItem>();
			mashupMap = new HashMap<String, MashupItem>();


			MashupItem item;
			List<Element> templateLists = xml.selectNodes("/mashup/item");
			for( Element el : templateLists){
				item = new MashupItem();

				item.setMashup_id(Integer.parseInt(el.getChild("mashupid").getText().trim()));
				item.setVendorName( el.getChild("vendorName").getText().trim() );
				item.setMashupName( el.getChild("mashupName").getText().trim() );
				item.setDisplayTitle( el.getChild("displayTitle").getText().trim() );
				item.setDescription( el.getChild("description").getText().trim() );
				item.setViewPage( el.getChild("viewPage").getText().trim() );
				item.setEnabled( el.getChild("enable").getText().trim().equalsIgnoreCase("Y") ? true: false );
				item.setApiKey( el.getChild("apiKey").getText().trim() );
				item.setApiBaseUri( el.getChild("apiBaseUri").getText().trim() );

				item.setDefaultParam( el.getChild("defaultParam").getText().trim() );
				item.setApiUrl( el.getChild("apiUrl").getText().trim() );
				item.setRequestEncoding( el.getChild("requestEncoding").getText().trim() );

				item.setResponseEncoding(el.getChild("responseEncoding").getText().trim());

				items.add(item);

				mashupMap.put(el.getChild("mashupid").getText().trim(), item);
			}


		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public MashupItem getItem(String key){
		if(mashupMap == null)
			mashupMap = new HashMap<String, MashupItem>();
		return this.mashupMap.get(key);
	}

	public List<MashupItem> getItems() {
		return items;
	}

	public void setItems(List<MashupItem> items) {
		this.items = items;
	}
}
